<!DOCTYPE html> 
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1"> 
	<title>Squash results</title> 
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
	<script src="//code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js" type="text/javascript"></script>
	<script type="text/javascript">
	
		$(document).ready(function() {
			fetchAndShowAllUsers();
		});
		
		function fetchAndShowAllUsers() {
			$.ajax({
				url : "/rest/user",
				type: 'GET',
			    contentType: 'application/json',
			    success: function (allUsers) {
			    	var html = "";
			    	for (var i in allUsers) {
			    		var user = allUsers[i];
			    		html += "<b>" + user.name  + "</b> (" + user.email + ")<br>";
					}
					$('#allUsersList').html(html);
				},
				error: function (xhr, ajaxOptions, thrownError) {
					return function () {
			      		showPopup('Failure', 'Failed to get all users: ' + xhr.responseText);
			      	}(xhr, ajaxOptions, thrownError, user);
				}
			});
		}
	
		function createUser() {
			var user = {};
			user.name = $('#newUserName').val();
			user.email = $('#newUserEmail').val();
			callCreateUser(user);
		}
		
		function callCreateUser(user) {
			$.ajax({
				url : "/rest/user",
				type: 'POST',
			    contentType: 'application/json',
			    data: JSON.stringify(user),
			    dataType: 'json',
			    success: function (response) {
			    	return function () {
			    		$('#newUserName').val('');
						$('#newUserEmail').val('');
			      		showPopup('Success', 'User '+ user.name +' ('+ user.email +') is created!');
			      		fetchAndShowAllUsers();
						$.mobile.changePage( "#listUsersPage" );
			      	}(response, user);
				},
				error: function (xhr, ajaxOptions, thrownError) {
					return function () {
			      		showPopup('Failure', 'Failed to create user '+ user.name +' ('+ user.email +'): ' + xhr.responseText);
			      	}(xhr, ajaxOptions, thrownError, user);
				}
			});
		}
		
		function showPopup(header, text) {
			$("#popupHeader").html(header);
			$("#popupText").html(text);
			$("#lnkPopup").click();
		}
		
		
	</script>
</head> 

	
<body> 

<div data-role="page" id="listUsersPage">

	<div data-role="header">
		<h1>Users</h1>
	</div>

	<div data-role="content">
		<a href="#addUserPage" data-rel="dialog" data-transition="pop" data-role="button">Create new user</a>	
		<p id=allUsersList></p>
	</div>
</div>

<div data-role="page" id="addUserPage">

	<div data-role="header">
		<h1>Create new user</h1>
	</div>

	<div data-role="content" >	
		<div data-role="fieldcontain">
            <fieldset data-role="controlgroup">
                <label for="newUserName">
                    Name
                </label>
                <input name="" id="newUserName" placeholder="" value="" type="text" />
            </fieldset>
        </div>
        <div data-role="fieldcontain">
            <fieldset data-role="controlgroup">
                <label for="newUserEmail">
                    E-mail
                </label>
                <input name="" id="newUserEmail" placeholder="" value="" type="text" />
            </fieldset>
        </div>		
		<a href="javascript:createUser()" data-role="button">Create</a>	
	</div>
</div>

<a id='lnkPopup' href="#popup" data-rel="dialog" data-transition="pop" style='display:none;'></a>

<div data-role="popup" id="popup">

	<div data-role="header" data-theme="e">
		<h1 id='popupHeader'></h1>
	</div>

	<div data-role="content" data-theme="d">	
		<p id='popupText'></p>		
	</div>
</div>

</body>
</html>